<!doctype html>
<html>
	<head>
		<title>Data Reports</title>
		<meta http-equiv="Content-Type" content="text/html; charset=utf8" />
		<link href='css/bootstrap.min.css' rel="stylesheet" />
		<link href="css/style.css" type="text/css" rel="stylesheet"/>
    	<script type="text/javascript" src="js/jquery.min.js"></script>
		<script src="js/lang.js" type="text/javascript" charset="utf8"></script>
		<script src="js/common.js" type="text/javascript" charset="utf8"></script>
	</head>
	<body style="background-color: #F5F5F5;">
		
    	<div class="container-fluid">
			<div class="col-sm-12">
				<div class="col-sm-3"></div>
				<div class="col-sm-5">
					<form role="form" class="ng-pristine ng-valid">

						<div class="form-group">
							<label>DataSource</label>
							<select id="dataSources" name="dataSources" class="form-control">
								<option value=""></option>
							</select>
						</div>
						<div class="form-group">
							<label>Code</label>
							<input id="configCode" type="text" class="form-control"/>
						</div>

						<div class="form-group">
							<label>Name</label>
							<input id="reportName" type="text" class="form-control"/>
						</div>

						<div class="form-group">
							<label>Description</label>
							<input id="reportDesc" type="text" class="form-control"/>
						</div>

						<div class="form-group">
							<label>Format View</label>
							<textarea class="form-control" style='min-height:300px;border:1px #A8C7CE solid;line-height:20px;font-size:14px;overflow-y: auto' id="formattedSql"></textarea>
						</div>
						<div class="form-group">
							<div id="errorInfo" style="color: red;"></div>
						</div>
						<br/>
						<div class="container">
							<a class="btn btn-success" href="javascript:druid.reportAdd.ajaxRequestForSaveBasicInfo();">Save</a>
							<a class="btn btn-primary" href="javascript:location.href='index.html'">Close</a>
						</div>

					</form>
				</div>
				<div class="col-sm-4"></div>
			</div>
    	</div>
    	<script type="text/javascript">
    	$.namespace("druid.reportAdd");
    	druid.reportAdd = function () {
    		return  {
    			init : function() {
    				druid.common.buildHead(2);
                    this.ajaxRequestForDataSources();
    			},
				ajaxRequestForDataSources:function () {
                    $.ajax({
                        type: 'POST',
                        url: 'datasource.json',
                        success: function(data) {
                            var datasourceInfo = data.Content;
                            if (datasourceInfo == null)
                                return;
                            for(var i=0;i<datasourceInfo.length;i++){
                                var selected=$("#dataSources").val()==datasourceInfo[i]['configCode'];
                                if(selected){
                                    $("#dataSources").append("<option value='"+datasourceInfo[i]['configCode']+"' selected='"+selected+"'>"+datasourceInfo[i]['configName']+"</option>");
                                }else {
                                    $("#dataSources").append("<option value='"+datasourceInfo[i]['configCode']+"'>"+datasourceInfo[i]['configName']+"</option>");
                                }
                            }
                        },
                        dataType: "json"
                    });
                },
                ajaxRequestForSaveBasicInfo : function() {
    			    var data={
                        configCode:$('#configCode').val(),
    			        reportName:$('#reportName').val(),
                        reportDesc:$('#reportDesc').val(),
                        sqlContent:$('#formattedSql').val(),
                        dbCode:$('#dataSources').val()
                    };

                    $.ajax({
                        type: 'POST',
                        url: 'report-save.json',
						data:data,
                        success: function(data) {
							if(data.ResultCode==1){
								var id = data.Content;
								if (id == null)
									return;
								location.href = "report-detail.html?reportId="+id;
							}else{
								$("#errorInfo").text(data.Content);
							}
                        },
                        dataType: "json"
                    });
                }
    		}
    	}();

    	$(document).ready(function() {
    		druid.reportAdd.init();
    	});
    	</script>
	</body>
</html>
